import {
    Service
} from './service.model'
import Toast from '@vant/weapp/toast/toast'
const service = new Service()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        cityName: '全国',
        chinaData: {
            confirmedCount: 0,
            confirmedIncr: "+0",
            curedCount: 0,
            curedIncr: "+0",
            currentConfirmedCount: 0,
            currentConfirmedIncr: "+0",
            deadCount: 0,
            deadIncr: "+0",
            suspectedCount: 0,
            suspectedIncr: "+0"
        },
        areaData: {
            confirmedCount: 0,
            confirmedIncr: "+0",
            curedCount: 0,
            curedIncr: "+0",
            currentConfirmedCount: 0,
            currentConfirmedIncr: "+0",
            deadCount: 0,
            deadIncr: "+0",
            suspectedCount: 0,
            suspectedIncr: "+0"
        },
        type: 'china',
        sos: '122'
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad() {
        let that = this
        that._loadData()
    },

    _loadData() {
        let that = this,
            userLocation = service.getStorage('userLocation')
        that.setData({
            cityName: userLocation.city
        })
        that.getChinaData()
        that.getAreaData(userLocation)
    },

    /**
     * 获取全国疫情数据
     */
    getChinaData() {
        let that = this
        service.getChinaData().then((res) => {
            that.setData({
                chinaData: res.data
            })
        })
    },

    /**
     * 获取本地疫情数据
     * @param {*} userLocation 用户地址
     */
    getAreaData(userLocation) {
        let that = this
        service.getAreaData(userLocation).then((res) => {
            that.setData({
                areaData: res.data
            })
        }).then(() => {
            wx.hideLoading()
            wx.stopPullDownRefresh()
            wx.hideNavigationBarLoading()
        })
    },

    /**
     * 切换标签
     * @param {*} options
     */
    onSwitchType(options) {
        let that = this
        that.setData({
            type: options.detail.name
        })
    },

    /**
     * 点击违章查询
     */
    onTapViolation() {
        let that = this
        Toast.fail('功能即将开放')
    },

    /**
     * 点击驾照查分
     */
    onTapScore() {
        let that = this
        Toast.fail('功能即将开放')
    },

    /**
     * 点击其他功能
     */
    onTapOther() {
        let that = this
        Toast.fail('功能即将开放')
    },

    /**
     * 是否登录
     * @param {*} url /pages/violation/violation
     */
    isLogin(url) {
        let that = this
        Toast.fail('功能即将开放')
        // // 是否授权
        // wx.getSetting({
        //     success: (res) => {
        //         if (res.authSetting['scope.userInfo']) {
        //             let userInfo = service.getStorage('userInfo')
        //             if (userInfo.member >= 0) {
        //                 wx.navigateTo({
        //                     url: url
        //                 })
        //             } else {
        //                 Toast.fail('等级不足')
        //             }
        //         } else {
        //             Toast({
        //                 type: 'fail',
        //                 message: '请先登录',
        //                 onClose: () => {
        //                     wx.switchTab({
        //                         url: '/pages/my/my',
        //                     })
        //                 },
        //             })
        //         }
        //     }
        // })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.getTabBar().init() // 切换标签
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        wx.showLoading({
            title: '正在加载数据',
        })
        let that = this
        wx.showNavigationBarLoading()
        that._loadData()
    }
})